<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Fund Valuation Analysis</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        table { border-collapse: collapse; width: 80%; }
        th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .form-group { margin: 10px 0; }
        .btn { padding: 5px 10px; text-decoration: none; color: white; background: #007bff; }
    </style>
</head>
<body>
<h1>Fund Undervaluation Rate Query</h1>

<!-- Search Form -->
<form onsubmit="query(event)">
    <div class="form-group">
        <label>Fund Code:</label>
        <input type="text" id="fundCode" required />
    </div>
    <div class="form-group">
        <label>Unit NAV Threshold:</label>
        <input type="number" id="unitNav" step="0.0001" required />
    </div>
    <button type="submit">Query</button>
    &nbsp;
    <a href="/" class="btn">index页</a>
</form>

<!-- Result Table -->
<div id="resultContainer" style="display: none; margin-top: 20px;">
    <h2>Results</h2>
    <table>
        <thead>
        <tr>
            <th>基金代码</th>
            <th>基金名称</th>
            <th>区间</th>
            <th>起始日期</th>
            <th>低于历史估值 (%)</th>
        </tr>
        </thead>
        <tbody id="resultBody"></tbody>
    </table>
</div>

<script>
    function query(e) {
        e.preventDefault();
        const fundCode = document.getElementById('fundCode').value;
        const unitNav = document.getElementById('unitNav').value;

        // 显示加载状态（可选）
        document.getElementById('resultContainer').style.display = 'block';
        document.getElementById('resultBody').innerHTML = '<tr><td colspan="5">加载中...</td></tr>';

        // ✅ 调用 REST API
        fetch(`/api/fund/valuation?fundCode=${fundCode}&unitNav=${unitNav}`)
            .then(res => {
                if (!res.ok) throw new Error('Network response was not ok');
                return res.json();
            })
            .then(data => {
                const tbody = document.getElementById('resultBody');
                tbody.innerHTML = '';
                data.forEach(item => {
                    const tr = `
                        <tr>
                            <td>${item.fundCode}</td>
                            <td>${item.fundName}</td>
                            <td>${item.period}</td>
                            <td>${item.startDate}</td>
                            <td>${item.undervaluationRate}</td>
                        </tr>`;
                    tbody.innerHTML += tr;
                });
            })
            .catch(err => {
                document.getElementById('resultBody').innerHTML =
                    `<tr><td colspan="5" style="color:red;">查询失败: ${err.message}</td></tr>`;
            });
    }


</script>
</body>
</html>
